package com.bieyang.anjia.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import cn.jpush.android.api.JPushInterface;

import com.bieyang.anjia.R;
import com.bieyang.anjia.refresh.BasicHandler;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 
 * 如果是第一次进入app，进入欢迎界面；如果登陆过主机，直接获取账号登陆到主机；如果是注销过，则进入登陆界面
 * */
public class AcGuid extends BaseActivity {

	private View view1, view2, view3;
	private ViewPager viewPager;
	private List<View> viewList;
	private Button enter_button;
	private Intent intent;
	private Context con;
 

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		JPushInterface.init(getApplicationContext());
//		
//		// FIXME 注：以下操作必须在第一个启动界面中添加
//				MobclickAgent.setDebugMode(true);
//				MobclickAgent.openActivityDurationTrack(false);
//				MobclickAgent.updateOnlineConfig(this);
//		
		con = this;
		App.mContext = getApplicationContext();
		setContentView(R.layout.ac_guid);

		if (SharedPrefsUtil.getBooleanValue(con, "firstLogin", true)) {
			SharedPrefsUtil.putValue(con, "firstLogin", false);
		} else {
			if (SharedPrefsUtil.getBooleanValue(con, "loginMain", false)) {
				startActivity(new Intent(con, AcLogo.class));
			} else {
				startActivity(new Intent(con, AcTiYan.class));
			}
		 
			finish();
		}
		BasicHandler.init(this.getApplication());
		initView();
 
	}

	@SuppressWarnings({ "static-access", "deprecation" })
	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.viewpager);

		view1 = findViewById(R.layout.layout1);
		view2 = findViewById(R.layout.layout2);
		view3 = findViewById(R.layout.layout3);

		LayoutInflater lf = getLayoutInflater().from(this);
		view1 = lf.inflate(R.layout.layout1, null);
		view2 = lf.inflate(R.layout.layout2, null);
		view3 = lf.inflate(R.layout.layout3, null);

		viewList = new ArrayList<View>();//
		viewList.add(view1);
		viewList.add(view2);
		viewList.add(view3);

		PagerAdapter pagerAdapter = new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {

				return arg0 == arg1;
			}

			@Override
			public int getCount() {

				return viewList.size();
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				container.removeView(viewList.get(position));

			}

			@Override
			public int getItemPosition(Object object) {

				return super.getItemPosition(object);
			}

			// @Override
			// public CharSequence getPageTitle(int position) {
			//
			// return titleList.get(position);
			// }

			@Override
			public Object instantiateItem(ViewGroup container, int position) {

				container.addView(viewList.get(position));

				if (position == 2) {
					View view = viewList.get(position);
					enter_button = (Button) view.findViewById(R.id.button1);
					enter_button.setOnClickListener(new OnClickListener() {

						public void onClick(View v) {
							Log.d("click", "clicked");
							intent = new Intent(AcGuid.this, AcTiYan.class);
							startActivity(intent);
							finish();
						}
					});

				}
				return viewList.get(position);
			}

		};
		viewPager.setAdapter(pagerAdapter);
		final ImageView[] imgs = { (ImageView) findViewById(R.id.first),
				(ImageView) findViewById(R.id.second),
				(ImageView) findViewById(R.id.third) };
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (2 == arg0) {
					for (int i = 0; i < imgs.length; i++) {
						imgs[i].setImageBitmap(null);
					}
				} else {
					for (int i = 0; i < imgs.length; i++) {
						if (arg0 == i) {
							imgs[i].setImageResource(R.drawable.dot_now);
						} else {
							imgs[i].setImageResource(R.drawable.dot_next);
						}
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

}
